﻿/**************************************************************************
创建日期:	2019/7/22 10:13:07     
作	  者:	张存
邮 	  箱:	zhangcunliang@126.com
创建时间:	
Copyright (c)  ZhCun.cn

描	述：
记	录：
***************************************************************************/
using Microsoft.AspNetCore.Mvc;
using System;
using System.Collections.Generic;
using System.Text;
using ZhCun.WebCommon.AL;

namespace ZhCun.WebCommon.WebPage
{
    /// <summary>
    /// 基于layui的增删查改Controller
    /// </summary>
    public abstract class PageLayuiCRUDController : PageAuthorizeController
    {
        protected abstract string ApiControllerName { get; }

        protected virtual string AddUrl { get { return $"/api/{ApiControllerName}/add"; } }

        protected virtual string UpdateUrl { get { return $"/api/{ApiControllerName}/update"; } }

        protected virtual string DelUrl { get { return $"/api/{ApiControllerName}/del"; } }

        protected virtual string ListUrl { get { return $"/api/{ApiControllerName}/getlist"; } }

        /// <summary>
        /// 返回Edit页面之前的调用，如设置ViewModel 等重写该方法
        /// </summary>
        [NonAction]
        protected virtual IActionResult GetEditView()
        {
            return View();
        }
        /// <summary>
        /// 返回List页面之前的调用，如设置ViewModel 
        /// </summary>
        /// <returns></returns>
        [NonAction]
        protected virtual IActionResult GetListView()
        {
            return View();
        }

        /// <summary>
        /// 用户列表
        /// </summary>
        public IActionResult List()
        {
            ViewBag.DelUrl = DelUrl;
            ViewBag.ListUrl = ListUrl;
            //return View();
            return GetListView();
        }


        /// <summary>
        /// 编辑窗口,type:add or update
        /// </summary>
        [NonRoleVerify]
        public IActionResult Edit(string type)
        {
            if ("add".Equals(type, StringComparison.CurrentCultureIgnoreCase))
            {
                ViewBag.SaveUrl = AddUrl;
            }
            else
            {
                ViewBag.SaveUrl = UpdateUrl; //updateUrl;
            }
            return GetEditView();
        }
    }

    /// <summary>
    /// 基于layui的增删查改Controller 带AL对象
    /// </summary>
    /// <typeparam name="TAL"></typeparam>
    public abstract class PageLayuiCRUDController<TAL> : PageLayuiCRUDController
        where TAL : BaseAL, new()
    {
        TAL _AL;
        /// <summary>
        /// 当前控制器所依赖的AL对象
        /// </summary>
        public TAL ALObj
        {
            get
            {
                if (_AL == null)
                {
                    _AL = CreateALObject<TAL>();
                }
                return _AL;
            }
        }
    }
}